package com.geek.gkfgw.biz.redis;

import com.geek.gkfgw.framework.cache.redis.AbstractRedisListener;
import com.geek.gkfgw.framework.cache.redis.RedisListener;
import com.geek.gkfgw.framework.cache.util.EhcacheUtils;
import com.geek.gkfgw.service.token.bean.RedisTokenMsg;
import com.geek.gkfgw.service.token.bean.TokenInfo;
import com.geek.gkfgw.util.copy.ShallowCopyUtils;
import com.geek.gkfgw.util.enums.redis.RedisTopicEnum;
import com.geek.gkfgw.util.json.JacksonUtils;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.stereotype.Component;

/**
 * <h1>接收令牌信息的 Redis 消息监听器。</h1><br/>
 *
 * <h1><br/><a href="https://u.geekbang.org/subject/arch3/100614201">Java 业务架构实战营：https://u.geekbang.org/subject/arch3/100614201</a></h1><br/><h1><a href="https://gitee.com/ylimhhmily/BizArchPracticeCamp">Java 业务架构实战营代码地址：https://gitee.com/ylimhhmily/BizArchPracticeCamp</a></h1><br/><h1><a href="https://u.geekbang.org/subject/java4th/1001148?source=app_share">高级 Java 工程体系课 2023 版：https://u.geekbang.org/subject/java4th/1001148?source=app_share</a></h1><br/><h1><a href="https://time.geekbang.org/column/intro/100312101?tab=catalog">Dubbo 源码剖析与实战：https://time.geekbang.org/column/intro/100312101?tab=catalog</a></h1>
 *
 * @author hmilyylimh
 * ^_^
 * @version 0.0.1
 * ^_^
 * @date 2023/10/15 16:40
 */
@Component
public class RecvTokenInfoRedisListener extends AbstractRedisListener<RedisTokenMsg> implements RedisListener {

    /** <h2>令牌存储在 ehcache、redis 中 key 的前缀</h2> **/
    public static final String CACHE_TOKEN_PREFIX = "gkfgw:access_token:";

    @Override
    public MessageListener getMessageListener() {
        return this;
    }

    @Override
    public String getTopicName() {
        return RedisTopicEnum.ACCESS_TOKEN.getCode();
    }

    @Override
    public void doRecvMsg(RedisTokenMsg msgObject) {
        TokenInfo tokenInfo = ShallowCopyUtils.copyBeanProperties(msgObject, new TokenInfo());
        EhcacheUtils.set(CACHE_TOKEN_PREFIX + msgObject.getAccessToken(), JacksonUtils.toJson(tokenInfo));
    }
}